package com.itheima.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.RedisConstant;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.QiniuUtils;

import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
public class ClearImpJob {

    @Autowired
    JedisPool jedisPool;

    @Reference
    OrderSettingService orderSettingService;

    /**
     * 清理垃圾图片
     *      1. 获取要清理的垃圾图片的名称
     *      2. 删除七牛云服务器上的图片
     *      3. 删除redis中的垃圾图片的名称
     */
    public void clear(){

        //删除ordersetting

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Date date = new Date();
        Calendar instance = Calendar.getInstance();
        instance.setTime(date);
        instance.add(Calendar.MONTH,1);
        Date time = instance.getTime();
        String t = simpleDateFormat.format(time);
        orderSettingService.delete(t);

        // 1. 获取要清理的垃圾图片的名称

        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        for (String s : sdiff) {
            //2. 删除七牛云服务器上的图片
            QiniuUtils.deleteFileFromQiniu(s);
            //3. 删除redis中的垃圾图片的名称
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES, s);
        }



    }
}
